//
//  MainViewController.swift
//  新浪微博
//
//  Created by 李国伟 on 15/12/6.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        setUpComposeButton()
    }
}

///  添加子控制器，设置子控件
extension MainViewController
{
    //添加撰写控件
    private func setUpComposeButton()
    {
        /// 防止出现容错
        let W:CGFloat = tabBar.bounds.width/CGFloat(childViewControllers.count)
        let button = UIButton(imageNamed: "tabbar_compose_icon_add", backImageNamed: "tabbar_compose_button")
        
        button.frame = CGRectInset(tabBar.bounds,2*W, 0)
        button.addTarget(self, action: "modalCompose", forControlEvents: UIControlEvents.TouchUpInside)
        tabBar.addSubview(button)
    }
    
    /**
     添加到主控制器
     */
   private func addChildViewControllers()
   {
    
    addChildViewController(HomeTableViewController(), title: "首页", image: "tabbar_home")
       addChildViewController(MessageTableViewController(), title: "消息", image: "tabbar_message_center")
    addChildViewController(UIViewController())
    addChildViewController(DiscoverTableViewController(), title: "发现", image: "tabbar_discover")
    addChildViewController(ProfileTableViewController(), title: "我", image: "tabbar_profile")
    }
    
    //添加单个子控制器的方法
   private func addChildViewController(vc:UIViewController,title:String,image:String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: image)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    //modal添加控制器
    @objc private func modalCompose() {
    
        let vc: UIViewController
        if UserAccountViewModel.shareModel.userlogin{
            vc = ComposeController()
            vc.view.backgroundColor = UIColor.whiteColor()
            presentViewController(vc, animated: true, completion: nil)
        }else{
            vc = OauthViewController()
            let nav = UINavigationController(rootViewController: vc)
            presentViewController(nav, animated: true, completion: nil)
        }
    }
}
